package com.action;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.pojo.Lunboimgs;
import com.pojo.Question;
import com.service.LunboService;
import com.service.WendaService;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 招生问答管理接口
 */
@RestController
@RequestMapping("/lunbo")
public class LunboAction {

    @Value("${upload.dir}")
    private String upload ;

    @Value("${upload.tmp.dir}")
    private String tmp ;
    /**
     * 分页查询轮播图
     * @param page
     * @param limit
     * @param question
     * @return
     */
    @Autowired
    private LunboService lunboService;
    @RequestMapping("/select")
    public ResponseBean selectPage(int page,int limit,String type){
        IPage iPage =lunboService.getPage(page, limit,type);
        return new ResponseBean(0, iPage.getTotal(), "成功", iPage.getRecords());
    }

    /**
     * 添加轮播图片
     * @param lunboimgs
     * @return
     */
    @PostMapping("/add")
    public ResponseBean add(@RequestBody Lunboimgs lunboimgs){
        lunboimgs.setImgstatus(1);
        if(StringUtil.isNull(lunboimgs.getTopicid())) {
            return ResponseBean.error("请提交轮播归属界面") ;
        }
        FileUtil.copy(tmp + lunboimgs.getImgsrc(), upload, true) ;
        boolean n = lunboService.save(lunboimgs);
        if(n)
            return  ResponseBean.ok("添加成功");
        else
            return  ResponseBean.error("添加失败");
    }
    /**
     * 修改轮播图片
     * @param lunboimgs
     * @return
     */
    @PostMapping("/update")
    public ResponseBean update(@RequestBody Lunboimgs lunboimgs){
        lunboimgs.setImgstatus(1);
        FileUtil.copy(tmp + lunboimgs.getImgsrc(), upload, true) ;
        boolean n = lunboService.updateById(lunboimgs);
        if(n)
            return  ResponseBean.ok("添加成功");
        else
            return  ResponseBean.error("添加失败");
    }
    /**
     * 修改轮播图的状态
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("/upStatus")
    public  ResponseBean upStatus(String id,Integer status){
        Lunboimgs lunboimgs = lunboService.getById(id);
        lunboimgs.setImgstatus(status);
        boolean f=  lunboService.updateById(lunboimgs);
        if(f)
            return  ResponseBean.ok("修改成功");
        else
            return  ResponseBean.error("修改失败");
    }
    /**
     * 删除轮播图
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public  ResponseBean upStatus(String id){
        Lunboimgs lunboimgs = lunboService.getById(id);
        boolean f=  lunboService.removeById(id);
        if(f)
            return  ResponseBean.ok("修改成功");
        else
            return  ResponseBean.error("修改失败");
    }
    /**
     * 删除轮播图
     * @param id
     * @return
     */
    @RequestMapping("/getone")
    public  ResponseBean getone(String id){
        Lunboimgs lunboimgs = lunboService.getById(id);
        if(lunboimgs!=null)
            return  new ResponseBean(200,"成功",lunboimgs);
        else
            return  ResponseBean.error("查询失败");
    }
}
